# 开发环境
FROM ubuntu:latest AS dev

# 设置时区和语言环境
ENV TZ=Asia/Shanghai
ENV LANG=C.UTF-8
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 安装基础工具和依赖
RUN apt-get update && apt-get install -y \
    curl \
    git \
    vim \
    wget \
    gnupg \
    openssh-server \
    iproute2 \
    iputils-ping \
    net-tools \
    build-essential \
    init \
    lsb-release \
    && rm -rf /var/lib/apt/lists/*

# 配置 SSH
RUN mkdir -p /var/run/sshd /var/log/ssh && \
    chmod 755 /var/run/sshd && \
    chmod -R 755 /var/log/ssh && \
    echo 'root:123456' | chpasswd && \
    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
    sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config && \
    sed -i 's/#AddressFamily any/AddressFamily any/' /etc/ssh/sshd_config && \
    sed -i 's/#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/' /etc/ssh/sshd_config && \
    echo "LogLevel DEBUG3" >> /etc/ssh/sshd_config && \
    echo "PidFile /var/run/sshd/sshd.pid" >> /etc/ssh/sshd_config

# 安装 MongoDB
RUN wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | apt-key add - && \
    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list && \
    apt-get update && \
    apt-get install -y mongodb-org && \
    mkdir -p /data/db /var/log/mongodb /var/run/mongodb && \
    chown -R mongodb:mongodb /data/db /var/log/mongodb /var/run/mongodb && \
    chmod 755 /data/db /var/log/mongodb /var/run/mongodb

# 配置 MongoDB 服务
RUN echo "net:\n  ipv6: true\n  bindIpAll: true\n  bindIp: 0.0.0.0,::,127.0.0.1,::1\nsystemLog:\n  destination: file\n  path: /var/log/mongodb/mongod.log\n  logAppend: true\nsecurity:\n  authorization: enabled\nprocessManagement:\n  fork: true\n  pidFilePath: /var/run/mongodb/mongod.pid\nstorage:\n  dbPath: /data/db" > /etc/mongod.conf && \
    echo "#!/bin/sh\nDESCRIPTION=\"MongoDB Database Server\"\nNAME=mongod\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nDEAMON=/usr/bin/mongod\nDEAMON_OPTS=\"--config /etc/mongod.conf\"\nPIDFILE=/var/run/mongodb/mongod.pid\n\ntest -x \$DEAMON || exit 0\n\n. /lib/lsb/init-functions\n\ncase \"\$1\" in\n  start)\n    log_daemon_msg \"Starting \$DESCRIPTION\" \"\$NAME\"\n    mkdir -p /var/run/mongodb\n    chown mongodb:mongodb /var/run/mongodb\n    start-stop-daemon --start --quiet --pidfile \$PIDFILE --exec \$DEAMON -- \$DEAMON_OPTS\n    log_end_msg \$?\n    ;;\n  stop)\n    log_daemon_msg \"Stopping \$DESCRIPTION\" \"\$NAME\"\n    start-stop-daemon --stop --quiet --pidfile \$PIDFILE --exec \$DEAMON\n    log_end_msg \$?\n    ;;\n  restart)\n    \$0 stop\n    \$0 start\n    ;;\n  status)\n    status_of_proc -p \$PIDFILE \$DEAMON \$NAME && exit 0 || exit \$?\n    ;;\n  *)\n    echo \"Usage: \$NAME {start|stop|restart|status}\" >&2\n    exit 1\n    ;;\nesac\n\nexit 0" > /etc/init.d/mongod && \
    chmod +x /etc/init.d/mongod && \
    update-rc.d mongod defaults

# 安装 Node.js 20.x LTS
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs && \
    npm install -g pnpm

# 创建项目目录结构
RUN mkdir -p /workspace/gps

# 设置工作目录
WORKDIR /workspace/gps

# 配置 pnpm
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable && corepack prepare pnpm@latest --activate

# 安装全局开发工具
RUN pnpm install -g @nestjs/cli typescript ts-node nodemon \
    prettier eslint typescript-eslint \
    cross-env

# 配置 git
RUN git config --global init.defaultBranch main && \
    git config --global core.autocrlf input && \
    git config --global core.editor "vim" && \
    git config --global pull.rebase false

# 配置 vim
RUN echo "set encoding=utf-8" >> ~/.vimrc

# 暴露端口
EXPOSE 22 3000 8080 9229 24678 27017

# 复制脚本文件
COPY scripts /usr/local/bin/scripts/
RUN chmod +x /usr/local/bin/scripts/*.sh

# 设置开发命令
ENTRYPOINT ["/usr/local/bin/scripts/entrypoint.sh"]
CMD ["start"] 